package cn.bugstack.mybatis.mapping;

import cn.bugstack.mybatis.scripting.LanguageDriver;
import cn.bugstack.mybatis.session.Configuration;
import org.apache.ibatis.annotations.ResultType;

import java.util.List;

/**
 *     <select id="queryUserInfo" parameterType="cn.bugstack.mybatis.test.po.User" resultType="cn.bugstack.mybatis.test.po.User">
 *         SELECT id, name
 *         FROM user
 *         where id = #{id}
 *     </select>
 *     映射sql
 */
public class MappedStatement {

    private Configuration configuration;
    private SqlCommandType sqlCommandType;

    private String id;
    private SqlSource sqlSource;

    private Class<?> resultType;

    private List<ResultMap> resultMaps;

    public LanguageDriver getLang() {
        return lang;
    }

    private LanguageDriver lang;

    // 内部类 Builder 实现建造者模式来创建 MappedStatement
    public static class Builder {
        private MappedStatement mappedStatement = new MappedStatement();

        public Builder(Configuration configuration,String id,SqlCommandType sqlCommandType,SqlSource sqlSource,Class<?> resultType) {
            mappedStatement.configuration = configuration;
            mappedStatement.id = id;
            mappedStatement.sqlCommandType = sqlCommandType;
            mappedStatement.sqlSource = sqlSource;
            mappedStatement.resultType = resultType;
            mappedStatement.lang = configuration.getLanguageRegistry().getDefaultDriver();

        }
        public MappedStatement build() {
            assert mappedStatement.id != null;
            return mappedStatement;
        }

        public String id() {
            return mappedStatement.id;
        }

        public void resultMaps(List<ResultMap> resultMaps) {
            mappedStatement.resultMaps = resultMaps;
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public SqlCommandType getSqlCommandType() {
        return sqlCommandType;
    }

    public void setSqlCommandType(SqlCommandType sqlCommandType) {
        this.sqlCommandType = sqlCommandType;
    }


    public SqlSource getSqlSource() {
        return sqlSource;
    }

    public void setSqlSource(SqlSource sqlSource) {
        this.sqlSource = sqlSource;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public Class<?> getResultType() {
        return resultType;
    }

    public List<ResultMap> getResultMaps() {
        return resultMaps;
    }
}
